package com.yangcangclub.trade.domain;

import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.trade.base.TradeBaseDomain;
import com.yangcangclub.trade.bean.TradeDetails;
import com.yangcangclub.trade.bean.TradeVo;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/2/27.
 */
@Component
public class TradeDetailsDomain extends TradeBaseDomain {

    public int insertTradeDetails(TradeDetails tradeDetails){
        super.getMasterJdbc().insert("trade.TradeDetailsMapper.insertTradeDetails",tradeDetails);
        int id = tradeDetails.getId();
        if(id == 0){
            //回滚
            throw new ApolloException(MessageEnum.TRADE_DETAILS_ADD_FAILURE);
        }
        return id;
    }

    /**根据订单号查询订单详细信息*/
    public List<TradeDetails> selectTradeDetailsByTradeNo(String tradeNo){
        Map m = Maps.newHashMap();
        m.put("tradeNo",tradeNo);
        m.put("sortColumns","create_date asc");
        return super.getSlaveJdbc().selectList("trade.TradeDetailsMapper.selectPageBreakByCondition",m);
    }

    /**根据条件查询订单信息*/
    public List<TradeDetails> selectPageBreakByCondition(Map m, int pageNum, int pageSize){
        if(pageNum<0||pageSize>50||pageSize<0){
            throw new ApolloException(MessageEnum.PARMA_RANGE_ERROR);
        }
        m.put("start",(pageNum-1)*pageSize);
        m.put("pageSize",pageSize);
        return super.getSlaveJdbc().selectList("trade.TradeDetailsMapper.selectPageBreakByCondition",m);
    }

    public int selectPageBreakByConditionCount(Map map){
        int total = super.getSlaveJdbc().selectOne("trade.TradeDetailsMapper.selectPageBreakByConditionCount",map);
        return total;
    }

    /**根据订单号更新订单详细状态
     * @param tradeNo
     * @param status
     * @return
     * */
    public boolean updateTradeDetailsStatusByTrandNo(String tradeNo,String status){
        Map m = Maps.newHashMap();
        m.put("tradeNo",tradeNo);
        m.put("status",status);
        int num = super.getMasterJdbc().update("trade.TradeDetailsMapper.updateTradeDetailsStatusByTrandNo",m);
        if(num == 0){
            throw new ApolloException(MessageEnum.ERROR_TRADE_DETAILS_UPDATE);
        }else{
            return true;
        }
    }

    /** 根据订单号和商品id更新订单详细状态
     * @param tradeNo
     * @param productId
     * @param status
     * @return
     */
    public boolean updateTradeDetailsStatusByTrandNoAndProductId(String tradeNo,int productId,String status){
        Map m = Maps.newHashMap();
        m.put("tradeNo",tradeNo);
        m.put("productId",productId);
        m.put("status",status);
        int num = super.getMasterJdbc().update("trade.TradeDetailsMapper.updateTradeDetailsStatusByTrandNoAndProductId",m);
        if(num == 0){
            throw new ApolloException(MessageEnum.ERROR_TRADE_DETAILS_UPDATE);
        }else{
            return true;
        }
    }

}
